package com.javase.collection;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Set 工具(业务实现)
 *
 * @author wangsh
 * @date 2018/12/18 10:28.
 */
public class SetUtil {
    /**
     * 读取 Set（For 循环实现）
     * 类型明确时直接读，类型不明确可用泛型
     *
     * @param
     * @return
     * @author wangsh
     * @source 2018/12/18 10:35
     **/
    public static void setRead2(Set<String> set) {
        for (Object obj : set) {
            if (obj == null) {
                System.out.println(obj);
                continue;
            }

            if (obj instanceof Integer) {
                int aa = (Integer) obj;
                System.out.println(aa);
            } else if (obj instanceof String) {
                String aa = (String) obj;
                System.out.println(aa);
            }
        }
    }

    /**
     * 读取 Set（迭代实现）
     *
     * @param
     * @return
     * @author wangsh
     * @source 2018/12/18 10:35
     **/
    public static void setRead1(Set<String> set) {
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String str = it.next();
            System.out.println(str);
        }
    }

    public static void main(String[] args) {
        Set set = new HashSet();
        set.add(null);

        int len = set.size();
        System.out.println(len);
        if (len > 0) {
            setRead2(set);
        }
    }
}
